package com.jlrdb.DAO;

import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Transaction;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.jlrdb.model.CellularBroadcast;
import com.jlrdb.model.CellularBroadcasts;

public class CellularBroadcastDAOImpl implements CellularBroadcastDAO {
	@Override
	public int addCellularBroadcast(CellularBroadcast cellularBroadcast) {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Transaction txn = datastore.beginTransaction();
		Entity cellulardata = new Entity("CellularBroadcastData");
		cellulardata.setProperty("Country", cellularBroadcast.getCountry());
		cellulardata.setProperty("Geographic_Region", cellularBroadcast.getGeographic_Region());
		cellulardata.setProperty("Cellular_Technology", cellularBroadcast.getCellular_Technology());
		cellulardata.setProperty("Data_Enhancement_Technology", cellularBroadcast.getData_Enhancement_Technology());
		cellulardata.setProperty("Information_Transfer_Rate", cellularBroadcast.getInformation_Transfer_Rate());
		cellulardata.setProperty("Start_Date", cellularBroadcast.getStart_Date());
		cellulardata.setProperty("End_Date", cellularBroadcast.getEnd_Date());
		cellulardata.setProperty("Frequency_Spectrum", cellularBroadcast.getFrequency_Spectrum());
		cellulardata.setProperty("Market_Penetration", cellularBroadcast.getMarket_Penetration());
		cellulardata.setProperty("Operators", cellularBroadcast.getOperators());
		cellulardata.setProperty("Source_Of_Reference", cellularBroadcast.getSource_Of_Reference());
		cellulardata.setProperty("Aditional_Contacts", cellularBroadcast.getAditional_Contacts());
		cellulardata.setProperty("Industry_Regulators", cellularBroadcast.getIndustry_Regulators());
		cellulardata.setProperty("Market_Confirmation", cellularBroadcast.getMarket_Confirmation());
		cellulardata.setProperty("Notes", cellularBroadcast.getNotes());
		cellulardata.setProperty("Problems", cellularBroadcast.getProblems());
		cellulardata.setProperty("Last_Date_Verified", cellularBroadcast.getLast_Date_Verified());
		datastore.put(cellulardata);
		txn.commit();
		return 0;
	}

	@SuppressWarnings("deprecation")
	@Override
	public CellularBroadcasts viewCellularBroadcasts(CellularBroadcast cellularQuery) {
		CellularBroadcasts cellularBroadcasts = new CellularBroadcasts();
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query cellularBroadcastData = null;
		cellularBroadcastData = new Query("CellularBroadcastData").addSort("Country", SortDirection.ASCENDING);
		if (cellularQuery.getCountry() != null)
			if (!cellularQuery.getCountry().isEmpty())
				cellularBroadcastData.addFilter("Country", Query.FilterOperator.EQUAL, cellularQuery.getCountry());
		if (cellularQuery.getGeographic_Region() != null)
			if (!cellularQuery.getGeographic_Region().isEmpty())
				cellularBroadcastData.addFilter("Geographic_Region", Query.FilterOperator.EQUAL, cellularQuery.getGeographic_Region());
		if (cellularQuery.getCellular_Technology() != null)
			if (!cellularQuery.getCellular_Technology().isEmpty())
				cellularBroadcastData.addFilter("Cellular_Technology", Query.FilterOperator.EQUAL, cellularQuery.getCellular_Technology());
		if (cellularQuery.getMarket_Confirmation() != null)
			if (!cellularQuery.getMarket_Confirmation().isEmpty())
				cellularBroadcastData.addFilter("Market_Confirmation", Query.FilterOperator.EQUAL, cellularQuery.getMarket_Confirmation());
		List<Entity> results = datastore.prepare(cellularBroadcastData).asList(FetchOptions.Builder.withDefaults());
		if (!results.isEmpty())
		{
			for (Entity result : results)
			{
				CellularBroadcast cellulardata = new CellularBroadcast();
				cellulardata.setGeographic_Region(result.getProperty("Geographic_Region").toString());
				cellulardata.setCountry(result.getProperty("Country").toString());
				cellulardata.setCellular_Technology(result.getProperty("Cellular_Technology").toString());
				cellulardata.setData_Enhancement_Technology(result.getProperty("Data_Enhancement_Technology").toString());
				cellulardata.setInformation_Transfer_Rate(result.getProperty("Information_Transfer_Rate").toString());
				cellulardata.setStart_Date(result.getProperty("Start_Date").toString());
				cellulardata.setEnd_Date(result.getProperty("End_Date").toString());
				cellulardata.setFrequency_Spectrum(result.getProperty("Frequency_Spectrum").toString());
				cellulardata.setMarket_Penetration(result.getProperty("Market_Penetration").toString());
				cellulardata.setOperators(result.getProperty("Operators").toString());
				cellulardata.setSource_Of_Reference(result.getProperty("Source_Of_Reference").toString());
				cellulardata.setAditional_Contacts(result.getProperty("Aditional_Contacts").toString());
				cellulardata.setIndustry_Regulators(result.getProperty("Industry_Regulators").toString());
				cellulardata.setMarket_Confirmation(result.getProperty("Market_Confirmation").toString());
				cellulardata.setNotes(result.getProperty("Notes").toString());
				cellulardata.setProblems(result.getProperty("Problems").toString());
				cellulardata.setLast_Date_Verified(result.getProperty("Last_Date_Verified").toString());
				cellulardata.setBroadcast_Id(result.getKey().getId());
				cellularBroadcasts.addCellularBroadcasts(cellulardata);
			}
		}
		return cellularBroadcasts;
	}

	@Override
	public CellularBroadcast getThisBroadcast(long broadcastId) {
		CellularBroadcast cellulardata = new CellularBroadcast();
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Key key = KeyFactory.createKey("CellularBroadcastData", broadcastId);
		Entity result;
		try
		{
			result = datastore.get(key);
			cellulardata.setGeographic_Region(result.getProperty("Geographic_Region").toString());
			cellulardata.setCountry(result.getProperty("Country").toString());
			cellulardata.setCellular_Technology(result.getProperty("Cellular_Technology").toString());
			cellulardata.setData_Enhancement_Technology(result.getProperty("Data_Enhancement_Technology").toString());
			cellulardata.setInformation_Transfer_Rate(result.getProperty("Information_Transfer_Rate").toString());
			cellulardata.setStart_Date(result.getProperty("Start_Date").toString());
			cellulardata.setEnd_Date(result.getProperty("End_Date").toString());
			cellulardata.setFrequency_Spectrum(result.getProperty("Frequency_Spectrum").toString());
			cellulardata.setMarket_Penetration(result.getProperty("Market_Penetration").toString());
			cellulardata.setOperators(result.getProperty("Operators").toString());
			cellulardata.setSource_Of_Reference(result.getProperty("Source_Of_Reference").toString());
			cellulardata.setAditional_Contacts(result.getProperty("Aditional_Contacts").toString());
			cellulardata.setIndustry_Regulators(result.getProperty("Industry_Regulators").toString());
			cellulardata.setMarket_Confirmation(result.getProperty("Market_Confirmation").toString());
			cellulardata.setNotes(result.getProperty("Notes").toString());
			cellulardata.setProblems(result.getProperty("Problems").toString());
			cellulardata.setBroadcast_Id(result.getKey().getId());
			cellulardata.setLast_Date_Verified(result.getProperty("Last_Date_Verified").toString());
		} catch (EntityNotFoundException e)
		{
			e.printStackTrace();
		}
		return cellulardata;
	}

	@Override
	public int updateCellularBroadcast(CellularBroadcast cellularBroadcast, long broadcastId) {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Transaction txn = datastore.beginTransaction();
		Entity cellulardata = null;
		Key key = KeyFactory.createKey("CellularBroadcastData", broadcastId);
		try
		{
			cellulardata = datastore.get(key);
		} catch (EntityNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		cellulardata.setProperty("Country", cellularBroadcast.getCountry());
		// cellulardata.setProperty("Geographic_Region",
		// cellularBroadcast.getGeographic_Region());
		cellulardata.setProperty("Cellular_Technology", cellularBroadcast.getCellular_Technology());
		cellulardata.setProperty("Data_Enhancement_Technology", cellularBroadcast.getData_Enhancement_Technology());
		cellulardata.setProperty("Information_Transfer_Rate", cellularBroadcast.getInformation_Transfer_Rate());
		cellulardata.setProperty("Start_Date", cellularBroadcast.getStart_Date());
		cellulardata.setProperty("End_Date", cellularBroadcast.getEnd_Date());
		cellulardata.setProperty("Frequency_Spectrum", cellularBroadcast.getFrequency_Spectrum());
		cellulardata.setProperty("Market_Penetration", cellularBroadcast.getMarket_Penetration());
		cellulardata.setProperty("Operators", cellularBroadcast.getOperators());
		cellulardata.setProperty("Source_Of_Reference", cellularBroadcast.getSource_Of_Reference());
		cellulardata.setProperty("Aditional_Contacts", cellularBroadcast.getAditional_Contacts());
		cellulardata.setProperty("Industry_Regulators", cellularBroadcast.getIndustry_Regulators());
		cellulardata.setProperty("Market_Confirmation", cellularBroadcast.getMarket_Confirmation());
		cellulardata.setProperty("Notes", cellularBroadcast.getNotes());
		cellulardata.setProperty("Problems", cellularBroadcast.getProblems());
		cellulardata.setProperty("Last_Date_Verified", cellularBroadcast.getLast_Date_Verified());
		datastore.put(cellulardata);
		txn.commit();
		return 0;
	}
}
